<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ page import="BusinessLogic.Exceptions.AuthenticationFailedException" %>
<%@ page import="BusinessLogic.ControllerFacade" %>
<%@ page import="Presentation.WebUI.LinksGenerator" %>
<%@ page import="Domain.DataStructures.IUserData" %>

<%
    ControllerFacade c;
    if ((session.getAttribute("c")) == null)
        session.setAttribute("c", new ControllerFacade());
    c = (ControllerFacade) session.getAttribute("c");

    /* choose action */
    String nextPage;
    String message = null;
    String action = request.getParameter("action");
    IUserData currentUser = null;

    try {
        currentUser = c.getCurrentUser();
        nextPage = "mainScreen.jsp";
    } catch (AuthenticationFailedException e) {
        nextPage = "login.jsp";
    }

    try {
        if (action != null) {
            if (action.equals("Error")) {
                message = request.getParameter("message");
            }

            if (action.equals("PreformLogin")) {
                String user = request.getParameter("username");
                String password = request.getParameter("password");
                c.login(user, password);
                currentUser = c.getCurrentUser();
                nextPage = "mainScreen.jsp";
            }

            if (action.equals("Register")) {
                nextPage = "register.jsp";
            }

            if (action.equals("PreformRegister")) {
                String user = request.getParameter("username");
                String first = request.getParameter("first");
                String last = request.getParameter("last");
                String password = request.getParameter("password");
                String email = request.getParameter("email");
                String vip = request.getParameter("vip");
                String acl = request.getParameter("acl");
                c.addNewUser(user, first, last, password, vip, email, acl);
                message = "Register Success";
            }

            if (action.equals("AddNewMovie")) {
                nextPage = "addNewMovie.jsp";
            }

            if (action.equals("PreformAddNewMovie")) {
                String movieName = request.getParameter("movieName");
                String director = request.getParameter("director");
                String year = request.getParameter("year");
                String length = request.getParameter("length");
                String country = request.getParameter("country");
                String genres = request.getParameter("genres");
                String actors = request.getParameter("actors");

                c.addNewMovie(movieName, director, year, Integer.parseInt(length), country, genres, actors);
                message = "Movie " + movieName + "Was  Added";
                nextPage = "mainScreen.jsp";
            }

            if (action.equals("GetRecommendation")) {
                nextPage = "getRecommendation.jsp";
            }

            if (action.equals("AddRecommendation")) {
				nextPage = "searchMovie.jsp?action=SearchMovie&addRec=true&method=UNRECOMMENDED&keyword="+currentUser.getUsername();
            }

            if (action.equals("PreformAddRecommendation")) {
                String movie = request.getParameter("movieName");
                String rank = request.getParameter("rank");
                String description = request.getParameter("description");
                c.addMovieRecommendation(movie, Integer.parseInt(rank), description);
                message = "Recommendation Added";
                nextPage = "mainScreen.jsp";
            }

            if (action.equals("ShowAllMovies")) {
                nextPage = "searchMovie.jsp?action=SearchMovie&method=ALL";
            }
            if (action.equals("ShowAllUsers")) {
                nextPage = "searchUser.jsp?action=SearchUser&method=ALL";
            }

            if (action.equals("Logout")) {
                c.logout();
                currentUser = null;
                nextPage = "login.jsp";
            }

            if (action.equals("ShowMovie")) {
                nextPage = "movie.jsp";
            }

            if (action.equals("ReportsMenu")) {
                nextPage = "reportsMenu.jsp";
            }

            if (action.equals("SearchMovie")) {
                nextPage = "searchMovie.jsp";
            }
            if (action.equals("SearchUser")) {
                nextPage = "searchUser.jsp";
            }

            if (action.equals("ShowUser")) {
                nextPage = "showUser.jsp";
            }
            if (action.equals("DeleteUser")) {
                nextPage = "mainScreen.jsp";
                String userName = request.getParameter("userName");
                c.deleteUser(userName);
                message = "User <span class=\"username\">" + userName + "</span> was successfully deleted";
            }
            if (action.equals("DeleteMovie")) {
                nextPage = "mainScreen.jsp";
                String movieName = request.getParameter("movieName");
                c.deleteMovie(movieName);
                message = "Movie <span class=\"moviename\">" + movieName + "</span> was successfully deleted";
            }

            if (action.equals("LoggedInUsers")) {
                nextPage = "searchUser.jsp?action=SearchUser&method=LOGGED";
            }
            if (action.equals("MostInMonth")) {
                nextPage = "lastMonthUsers.jsp";
            }
            if (action.equals("MostRatedMovies")) {
                nextPage = "mostRatedMovies.jsp";
            }
            if (action.equals("MovieRankingCharts")) {
                nextPage = "movieRankingCharts.jsp";
            }
            
            if (action.equals("MovieRankDistribution")) {
                nextPage = "movieRankDistribution.jsp";
            }
            if (action.equals("UsersRatedMovie")) {
                nextPage = "usersRatedMovie.jsp";
            }
			if (action.equals("RecommendMovie")) {
				nextPage = "recommendMovie.jsp";
            }
        }

    }
    catch (Exception e) {
        message = e.getMessage();
    }
%>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <title>Movie System</title>
    <link href="css/styles.css" rel="stylesheet" type="text/css" media="all" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>

<table id="main" border="0" cellpadding="0" cellspacing="0">
    <tr valign="top">
        <td id="sideImage"><img src="images/image1.jpg" width="116" height="459" alt="Movie System" /></td>
        <td>
            <table border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td><h1>Grupi's Movie System</h1></td>
                </tr>
                <tr>
                    <td id="stripe" align="center">
                        <%
                            if (message != null)
                                out.println(message);
                        %>
                        &nbsp;
                    </td>
                </tr>
                <tr><td>
                    <table id="content">
                        <tr><td>
                            <jsp:include page="<%=nextPage%>" flush="false"/>
                        </td></tr>
                    </table>
                </td></tr>
            </table>
        </td>
    </tr>
    <%
        if (!nextPage.equals("mainScreen.jsp") && !nextPage.equals("login.jsp")){
            String back = "<tr><td>&nbsp;</td>";
            back += "<td colspan=\"2\"><table><tr>";
            back += "<td id=\"back\">";
			back += "\t<a href=\"javascript:history.go(-1)\">back</a>\n";
			back += "</td>\n";
            back += "<td id=\"backtomain\">";
            back += "\t<a href=\"index.jsp\">back to main screen</a>\n";
            back += "</td>\n";
            back += "</tr></table>";
            back += "</td></tr>";
            out.println(back);
        }

        try {
            if( currentUser != null ){
                String showUsers = "<tr><td colspan=\"3\" id=\"users\">";
                showUsers += "<h4>Online users (" + c.getOnlineUsers().size() + ")</h4>";

                String users = "";
                for(String user: c.getOnlineUsers()){
                    if(!users.equals("")) users += ", ";
                    users += LinksGenerator.userLink(user);
                }

                showUsers += users;
                showUsers += "</td></tr>";

                out.println(showUsers);
            }
        }
        catch (Exception e) {
        }
    %>
    <tr>
        <td class="footer" colspan="2">
            <table border="0" cellpadding="0" cellspacing="0"><tr>
                <td>
                    <a href="http://validator.w3.org/check?uri=referer">
                        <img src="images/valid-xhtml10.png"
                             alt="Valid XHTML 1.0 Strict" style="border:0;width:88px;height:31px" />
                    </a>
                </td>
                <td>
                    Copyright &copy; 2007 <a href="mailto:wsep072@smartmtk.com">Grupi's Movie System</a>. All rights reserved.
                </td>
                <td>
                    <a href="http://jigsaw.w3.org/css-validator/check/referer">
                        <img style="border:0;width:88px;height:31px"
                             src="images/vcss.png"
                             alt="Valid CSS!" />
                    </a>
                </td>
            </tr></table>
        </td>
    </tr>
</table>
</body>
</html>